//
//  MRScrollAndPageView.m
//  Marrymemo
//
//  Created by dllo on 15/9/12.
//  Copyright (c) 2015年 LiMuRan. All rights reserved.
//

#import "MRScrollAndPageView.h"
#import "UIImageView+WebCache.h"

@implementation MRScrollAndPageView


-(instancetype)initWithFrame:(CGRect)frame Arr:(NSMutableArray *)arr
{
    self = [super initWithFrame:frame];
    if (self) {
        self.myArr = arr;
        [self createScrollViewByArr:arr];
    }
    return self;
}

-(void)createScrollViewByArr:(NSMutableArray *)arr
{
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    self.pagingEnabled = YES;
    self.bounces = NO;
    
    // 如果数组的个数为零的话,给scrollView添加一个本地图片
    if (arr.count == 0) {
        
        UIImageView *ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        ImageView.image = [UIImage imageNamed:@"placeholder1"];
        ImageView.contentMode = UIViewContentModeScaleAspectFit;
        
        [self addSubview:ImageView];

    }
    // 如果数组的个数为1,直接放一个ImageView在scrollView上
    else if (arr.count == 1) {
        
        UIImageView *ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        [ImageView sd_setImageWithURL:[NSURL URLWithString:arr[0]]placeholderImage:[UIImage imageNamed:@"placeholder1"]];
        
        [self addSubview:ImageView];
        
    }
    // 如果数组的个数大于1,则给轮播图一个滚动范围,并且添加NSTimer 可以实现自动滚动和循环滚动
    else {
        
        self.contentSize = CGSizeMake((arr.count + 2) * self.frame.size.width, self.frame.size.height);
        
        self.contentOffset = CGPointMake(self.frame.size.width , 0);
        
        UIImageView *beginImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        
        [beginImageView sd_setImageWithURL:[NSURL URLWithString:arr[arr.count - 1]]placeholderImage:[UIImage imageNamed:@"placeholder1"]];
        
        [self addSubview:beginImageView];
        
        
        for (NSInteger i = 0; i < arr.count; i ++) {
            UIImageView *ImageView = [[UIImageView alloc] initWithFrame:CGRectMake((i + 1) * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
            
            [ImageView sd_setImageWithURL:[NSURL URLWithString:arr[i]]placeholderImage:[UIImage imageNamed:@"placeholder1"]];
            
            [self addSubview:ImageView];
        }
        
        UIImageView *endImageView = [[UIImageView alloc] initWithFrame:CGRectMake((arr.count + 1) * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        
        [endImageView sd_setImageWithURL:[NSURL URLWithString:arr[0]]placeholderImage:[UIImage imageNamed:@"placeholder1"]];
        
        
    
        [self addSubview:endImageView];
        [self addTimer];
    }
    
    NSLog(@"封装轮播图个数%ld",arr.count);
}

-(void)addTimer
{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    
    self.timer = timer;
    
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void)removeTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addTimer];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (self.contentOffset.x == self.frame.size.width * (self.myArr.count + 1)) {
        self.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    if (self.contentOffset.x == 0) {
        self.contentOffset = CGPointMake((self.myArr.count) * self.frame.size.width, 0);
    }
}

-(void)changeImage
{
    [self setContentOffset:CGPointMake(self.contentOffset.x + self.frame.size.width, 0) animated:YES];
    
    if (self.contentOffset.x == self.frame.size.width * (self.myArr.count + 1)) {
        self.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    
}



@end
